﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace GeoFly
{
    public class HortonHydroSimulate:HydroSimulate
    {

        public DateTime BeginTime;
        public DateTime EndTime;
        public void ReadStartEndTime()
        {
            StreamReader sr = new StreamReader(HortonHydroSimulate.path + "\\SysPara\\SysPara.txt");
            string[] line= sr.ReadLine().Split(new char[]{':','-'},StringSplitOptions.RemoveEmptyEntries);
            int year = Convert.ToInt16(line[1]);
            int month = Convert.ToInt16(line[2]);
            int day = Convert.ToInt16(line[3]);
            int hour = Convert.ToInt16(line[4]);
            BeginTime = new DateTime(year, month, day, hour, 0, 0);

            line = sr.ReadLine().Split(new char[] { ':', '-' }, StringSplitOptions.RemoveEmptyEntries);
            year = Convert.ToInt16(line[1]);
            month = Convert.ToInt16(line[2]);
            day = Convert.ToInt16(line[3]);
            hour = Convert.ToInt16(line[4]);
            EndTime=new DateTime(year, month, day, hour, 0, 0);
            sr.Close();
        }
        public HortonHydroSimulate(DEMRiverNet river)
        {
            this.River = river;
        }
        public void StormRunoffSim_Horton_Radar()
        {
            int rowCount = g_GridLayerPara.g_DemLayer.rowCount;
            int colCount = g_GridLayerPara.g_DemLayer.colCount;
            this.CellOutSurfFlux = new double[rowCount, colCount];
            this.CellOutLatFlux = new double[rowCount, colCount];
            this.CellOutBaseFlux = new double[rowCount, colCount];
            GridLayerInit();

            //下面这段程序由刘永和所加
            int n = River.CalcuOrderList.Count;
            CurrentSurfQ = new RouteFlux[n];
            CurrentLatQ = new RouteFlux[n];
            CurrentBaseQ = new RouteFlux[n];

            PreSurfQ = new RouteFlux[n];
            PreLatQ = new RouteFlux[n];
            PreBaseQ = new RouteFlux[n];
            for (int i = 0; i < n; i++)
            {
                CurrentSurfQ[i] = new RouteFlux();
                CurrentLatQ[i] = new RouteFlux();
                CurrentBaseQ[i] = new RouteFlux();
                PreSurfQ[i] = new RouteFlux();
                PreLatQ[i] = new RouteFlux();
                PreBaseQ[i] = new RouteFlux();
            }
            //计算间隔时段长度

            StreamWriter sw = new StreamWriter(HydroSimulate.path + "\\Output\\OutletQ.txt");
            sw.WriteLine("Order\tTotalQ\tSurfQ\tLatQ\tBaseQ\tDeepBaseQ");
            //初始化指定出口断面计算结果内存
            this.MyOutletQ = new SortedList<DateTime, OutletQ>();
            this.ReadStartEndTime();
            TimeSpan span=new TimeSpan(1,0,0);
            for(DateTime dateTime=BeginTime;dateTime<EndTime;dateTime+=span)
            {
                //取得该降雨时段的dn历
                int dn = dateTime.DayOfYear;// DateAndTime.GetDnInYear(dateTime.Year, dateTime.Month, dateTime.Day);
                //对该时段的降雨和气象进行插值
                string fileName=dateTime.Year.ToString("0000")+"_"+dateTime.Month.ToString("00")+"_"+dateTime.Day.ToString("00")+"_"+dateTime.Hour.ToString("00")+".txt";
                //=======================================================================================
                g_ClimatePara.pcpdata.m_gridLayer = g_GridLayerPara.g_DemLayer.AttributesCopy();
                string pathName = HortonHydroSimulate.path + "\\Climate\\RadarRain\\" + fileName;
                if (!File.Exists(pathName))
                    throw new Exception("雷达文件没找到");
                g_ClimatePara.pcpdata.m_gridLayer.ReadASC(pathName);
                //=======================================================================================
                g_ClimatePara.petdata.SpatialGridOut(dateTime);
             
                //计算该时段的小时数
                double dhr = 1;
                ProgressBar bar = new ProgressBar();
                bar.Text = "正在计算净雨量";
                bar.Show();
                for (int row = 0; row < rowCount; row++)
                {
                    for (int col = 0; col < colCount; col++)
                    {
                        //计算本单元的水量平衡
                        this.CalcCell_Horton(row, col, dhr, dateTime, dn);
                    }
                    bar.progressBar1.Value = (int)(row * 100.0 / rowCount);
                }
                bar.Close();//关闭计算净雨时的bar

                //下面开始汇流
                this.CalcRiverRouting_Liu(dateTime, dhr, dn);

                //输出流量值
                sw.Write(dateTime + "\t");
                OutletQ outData = this.MyOutletQ[dateTime];
                sw.Write(outData.m_pOutletQ.ToString("0.0000") + "\t");
                sw.Write(outData.m_pOutletSurfQ.ToString("0.0000") + "\t");
                sw.Write(outData.m_pOutletLatQ.ToString("0.0000") + "\t");
                sw.Write(outData.m_pOutletBaseQ.ToString("0.0000") + "\t");
                sw.Write(outData.m_pOutletDeepBaseQ.ToString("0.0000") + "\t");
                sw.WriteLine();
                sw.Flush();

                MidGridResultOut(dateTime, false);
            }
            sw.Close();
            RiverOutletQ(true);
        }
    }
}
